package com.hns.phone.pushMsg.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hns.phone.pushMsg.vo.PushMessageParamsVo;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 安卓列播
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Component
public class AndListNotification extends AndroidNotification {
	
	@Autowired
	private BasePushClient client;

	/**
	 * 发送信息动作
	 * @param params
	 * @throws Exception
	 * @see com.hns.can.pushMsg.util.AndroidNotification#sendMsg(com.hns.can.pushMsg.vo.PushMessageParamsVo)
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
	@Override
	public String sendMsg(PushMessageParamsVo params) throws Exception {
		setPredefinedKeyValue("appkey", params.getAppkey());
		setAppMasterSecret(params.getAppMasterSecret());
		this.setPredefinedKeyValue("type", "listcast");
		//组装发送设备tokens，多个device_token用英文逗号分隔
		this.setPredefinedKeyValue("device_tokens", params.getTokenStr());
		
		this.setTicker(params.getTitle());
		this.setTitle(params.getTitle());
		this.setText(params.getContent());
		
		this.setExtraField("userId", params.getUserId());
		this.setExtraField("msgDate", params.getMsgDate());
		this.goAppAfterOpen();
//		if("MOW00001".equals(params.getPushType())){
//			this.goAppAfterOpen();
//		}else if("MOW00002".equals(params.getPushType())){
//			this.goUrlAfterOpen(params.getWebUrl());
//		}
		this.setDisplayType(params.getDisplayType());
		
		//消息模式时，将发送内容设置在custom中
		if(AndroidNotification.DisplayType.MESSAGE.equals(params.getDisplayType())){
			JSONObject custom = new JSONObject();
			custom.put("title", params.getTitle());
			custom.put("content", params.getContent());
			this.goCustomAfterOpen(custom);
		}
		
		this.setProductionMode();
		
		this.setPlayLights(true);
		this.setPlayVibrate(true);
		this.setPlaySound(true);
		if(PubUtil.isNotEmpty(params.getDelayTime())){
			this.setStartTime(params.getDelayTime() + "00:00:00");
			String expireTime = DateTool.calculateByDate(params.getDelayTime(), 3);
			this.setExpireTime(expireTime + "23:59:59");
		}
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.setExtraField("crtTime", format.format(new Date()));
		
//		this.setTestMode();
		String taskId = client.sendMsg(this);
		return taskId;
	}
	
}
